<template>
  <div id="shopLineChart">
    <div ref="chart01" class="chart_box"></div>
    <div class="icon_checked">
      <el-checkbox-group v-model="checkedType" @change="checkedTypeFn">
        <el-checkbox label="0">无</el-checkbox>
        <el-checkbox label="1">添加商品</el-checkbox>
        <el-checkbox label="2">讲解商品</el-checkbox>
        <el-checkbox label="3">发放福袋</el-checkbox>
        <el-checkbox label="4">发放优惠卷</el-checkbox>
        <el-checkbox label="5">发放红包</el-checkbox>
      </el-checkbox-group>
    </div>
  </div>
</template>

<script>
let mychart = null;
import testIcon1 from '@/assets/meng/b5.png';
import testIcon2 from '@/assets/meng/b6.png';
import testIcon3 from '@/assets/meng/b7.png';
import testIcon4 from '@/assets/meng/b8.png';
import testIcon5 from '@/assets/meng/b9.png';
export default {
  name: 'liveLine',
  components: {},
  props: {
    xData: Array | Object,
    series1: Array | Object,
    series2: Array | Object,
    name1: String,
    name2: String
  },
  data() {
    return {
      checkedType: ['0'],
      iconList: [testIcon1, testIcon2, testIcon3, testIcon4, testIcon5]
    };
  },
  computed: {},
  watch: {
    xData: {
      handler(n, o) {
        this.drawLiveLine();
      },
      deep: true
    }
  },

  created() {
    // this.xD = this.xZ.map((element) => {
    //   return this.timestamp(element, "M/D");
    // });
  },
  mounted() {
    if (this.xData) {
      this.$nextTick(() => {
        this.drawLiveLine();
      });
    }
  },

  methods: {
    checkedTypeFn() {
      //底部选择事件函数
      if (!this.checkedType.length) {
        //如果无任何选中，默认为无
        this.checkedType = ['0'];
      } else if (this.checkedType.length > 1) {
        //只能选一个
        this.checkedType.splice(0, 1);
      }
      let iconDatas = [
        { value: 0 },
        { value: 0 },
        { value: 0 },
        { value: 0 },
        {},
        { value: 0 },
        {},
        { value: 0 },
        { value: 0 },
        {},
        { value: 0 },
        {},
        { value: 0 },
        { value: 0 },
        {},
        { value: 0 },
        {}
      ];
      this.setIconFn(iconDatas); //接口请求成功，把数据处理好后调用，如果无则为空对象
    },
    setIconFn(datas) {
      let iconSeries = {
        //图标配置
        // symbol:`image://${testIcon1}`,
        symbolSize: '20',
        symbolOffset: [0, -10],
        type: 'scatter',
        data: datas,
        tooltip: {
          //取消图标的tooltip提示
          show: false
        }
      };
      let checkedIdx = Number(this.checkedType[0]);
      if (checkedIdx) {
        iconSeries.symbol = `image://${this.iconList[checkedIdx - 1]}`;
        this.drawLiveLine(iconSeries);
      } else {
        this.drawLiveLine();
      }
    },
    drawLiveLine(iconListData) {
      let that = this;
      let mychart = null;
      mychart = that.$echarts.init(that.$refs.chart01);
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: 'line', // 默认为直线，可选为：'line' | 'shadow'
            lineStyle: {
              color: '#e5e5e5'
            }
          },
          formatter: function (params) {
            let result01 = '';
            params.forEach(function (item) {
              result01 +=
                item.marker + ' ' + item.seriesName + ' : ' + that.format_num(item.value) + '</br>';
            });
            return params[0].name + '</br>' + result01;
          }
        },
        legend: {
          icon: 'rect ',
          top: 28,
          itemWidth: 20,
          itemHeight: 3,
          itemGap: 30,
          textStyle: {
            color: '#555'
          }
        },
        grid: {
          left: '64',
          right: '74',
          bottom: '80',
          containLabel: false
        },
        xAxis: [
          {
            type: 'category',
            data: this.xData,
            boundaryGap: false, //X轴起始位置靠着Y轴
            //设置网格线颜色
            splitLine: {
              show: false,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            // x轴样式
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            },
            axisTick: {
              show: false
            },
            // x轴字体样式
            axisLabel: {
              // interval: 0,
              // rotate: 40,
              margin: 14,
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: this.name1,
            //设置网格线颜色
            nameTextStyle: {
              color: '#888',
              padding: [0, 0, 0, 45]
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              },
              formatter: function (value) {
                return that.format_num(value);
              }
            },
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            }
          },
          {
            type: 'value',
            name: this.name2,
            //设置网格线颜色
            nameTextStyle: {
              color: '#888',
              padding: [0, 0, 0, -55]
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              },
              formatter: function (value) {
                return that.format_num(value);
              }
            },
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            }
          }
        ],
        dataZoom: [
          {
            type: 'slider',
            realtime: true,
            start: 0,
            end: 100,
            backgroundColor: '#f0935e',
            fillerColor: 'rgba(255,255,255,0.2)',
            height: '20',
            border: '0',
            borderColor: '#f0935e'
            // borderColor:"#ddd",
          }
        ],
        series: [
          {
            name: this.name1,
            type: 'line',
            smooth: true, //true 为平滑曲线，false为直线
            symbol: 'none', //去掉折线上的小圆点
            data: this.series1,
            yAxisIndex: 0,
            itemStyle: {
              normal: {
                areaStyle: {
                  type: 'default',
                  //渐变色实现===
                  color: new this.$echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    //三种由深及浅的颜色
                    [
                      {
                        offset: 0,
                        color: '#ACCFFF'
                      },
                      {
                        offset: 1,
                        color: '#FAFAFB'
                      }
                    ]
                  )
                },
                color: '#2781FF'
                //柱子颜色
              }
            }
          },
          {
            name: this.name2,
            type: 'line',
            smooth: true, //true 为平滑曲线，false为直线
            symbol: 'none', //去掉折线上的小圆点
            yAxisIndex: 1,
            data: this.series2,
            itemStyle: {
              normal: {
                areaStyle: {
                  type: 'default',
                  //渐变色实现===
                  color: new this.$echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    //三种由深及浅的颜色
                    [
                      {
                        offset: 0,
                        color: '#FED1B4'
                      },
                      {
                        offset: 1,
                        color: '#FAFAFB'
                      }
                    ]
                  )
                },
                color: '#fd7f2c'
                //柱子颜色
              }
            }
          }
        ]
      };
      if (iconListData) {
        option.series.push(iconListData);
      } else {
        if (option.series.length > 2) {
          //如果大于2条折线说明有图标，则做删除操作
          option.series.splice(1, 2);
        }
      }
      mychart.setOption(option, true); //true,每次调用都重新渲染
      //   窗口自适应
      setTimeout(() => {
        let resizeFn = function () {
          mychart.resize();
        };
        window.addEventListener('resize', resizeFn);
        this.$once('hook:beforeDestroy', () => {
          mychart.dispose();
          mychart = null;
          window.removeEventListener('resize', resizeFn);
        });
      }, 200);
    }
  },
};
</script>
<style lang='less' scoped>
#shopLineChart {
  width: 100%;
  height: calc(100% - 35px);
  // margin-top: 36px;
  .chart_box {
    width: 100%;
    height: 100%;
    overflow: hidden;
    clear: both;
  }
  .icon_checked {
    text-align: center;
    .el-checkbox {
      margin-right: 25px;
    }
  }
}
</style>